﻿/*
 * Author: Roy Scheefhals
 * 
 * Description: Complex data object used by the fourier transform
 * */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LibraryCS
{
    /// <summary>
    /// Complex data object 
    /// Currently used for Fourier transforms
    /// </summary>
    public class Complex
    {
        public double real, imag;
        public float magnitude;

        /// <summary>
        /// Basic constructor ( set values to 0 )
        /// </summary>
        public Complex()
        {
            imag = 0;
            real = 0;
            magnitude = 0;
        }

        /// <summary>
        /// Constructor 
        /// </summary>
        /// <param name="x">Variable for the real part ( usually x )</param>
        /// <param name="y">Variable for the imaginary part ( usually y )</param>
        public Complex(double x, double y)
        {
            real = x;
            imag = y;
        }

        /// <summary>
        /// returns magnitude of the real and the imaginary part
        /// does not need to explicitly execute calcMag()
        /// </summary>
        /// <returns>The magnitude of this complex object</returns>
        public float Magnitude()
        {
            if (magnitude == 0.0) return ((float)Math.Sqrt(real * real + imag * imag));
            else return (float)magnitude;
        }

        /// <summary>
        /// Calculates the magnitude for this complex object
        /// </summary>
        public void calcMag()
        {
            magnitude = ((float)Math.Sqrt(real * real + imag * imag));
        }

        /// <summary>
        /// calculate and returns the Phase of this complex object
        /// Atan( imag / real )
        /// </summary>
        /// <returns>The phase of this complex object</returns>
        public float Phase()
        {
            return ((float)Math.Atan(imag / real));
        }
    }
}
